package com.weiwei.diancan.activity;

import android.graphics.Color;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.weiwei.diancan.Contant.ConfigMethod;
import com.weiwei.diancan.R;
import com.weiwei.diancan.adapter.AllOrderAdapter;
import com.weiwei.diancan.base.BaseActivity;
import com.weiwei.diancan.bean.LoginBean;
import com.weiwei.diancan.bean.OrderWapper;
import com.weiwei.diancan.cache.CacheConfig;
import com.weiwei.diancan.http.RetrofitFactory;
import com.weiwei.diancan.utils.ActivityManager;
import com.weiwei.diancan.utils.HttpUtils;
import com.weiwei.diancan.utils.LoggerUtils;
import com.weiwei.diancan.utils.OnSuccessListener;
import com.weiwei.diancan.utils.OrderUtils;
import com.weiwei.diancan.view.EmptyLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import weiwei.com.weiweilibrary.adapter.BaseQuickAdapter;
import weiwei.com.weiweilibrary.views.ToastUtil;

public class AllOrderActivity extends BaseActivity {

    private RecyclerView recycler;
    private AllOrderAdapter adater;
    private List<OrderWapper> oederList;
    private EmptyLayout emptyLayout;
    private SwipeRefreshLayout refresh;
    private TextView tv_price;
    private TextView tv_order_num;

    @Override
    public void getData() {
        if(oederList.size() == 0){
            emptyLayout.setVisibility(View.VISIBLE);
            emptyLayout.setErrorType(EmptyLayout.NO_DATA,EmptyLayout.NOT_DATA);
        }else{
            emptyLayout.setVisibility(View.GONE);
        }

        tv_order_num.setText(oederList.size()+"");
        double prce = 0;
        for(int i = 0;i<oederList.size();i++){
            OrderWapper orderWapper = oederList.get(i);
            prce+=Double.parseDouble(orderWapper.getOrder_price());
        }
        tv_price.setText("¥"+prce);
    }

    @Override
    public void setListeners() {
        adater.setOnRecyclerViewItemClickListener((view, position, data) -> {
            OrderWapper orderWapper = oederList.get(position);
            Bundle bundle = new Bundle();
            bundle.putString("tabListBean",orderWapper.getTab_id());
            bundle.putString("type",orderWapper.getType_id());
            openActivity(OrderDetailActivity.class,bundle);
        });

        refresh.setOnRefreshListener(() -> {
            onRefreshData();
        });

    }

    @Override
    public void initView() {

        tv_price = (TextView)findViewById(R.id.tv_price);
        tv_order_num = (TextView)findViewById(R.id.tv_order_num);
        recycler = (RecyclerView)findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(this));
        emptyLayout = (EmptyLayout)findViewById(R.id.emptyLayout);
        refresh = (SwipeRefreshLayout)findViewById(R.id.refresh);
        refresh.setColorSchemeColors(Color.parseColor("#B41B1B"));
        adater = new AllOrderAdapter(oederList);
        recycler.setAdapter(adater);
    }

    @Override
    public void initTitle() {
        titler.setText("全部订单");

        iv_left.setVisibility(View.VISIBLE);
        iv_left.setText("返回");
        iv_left.setOnClickListener(v->finish());
    }

    @Override
    public void setLayoutView() {
        setContentView(R.layout.activity_all_order);
        ActivityManager.getInstance().pushOneActivity(this);
        oederList = OrderUtils.getOederList(null,0);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityManager.getInstance().popOneActivity(this);
    }

    /**
     *
     *   刷新
     */
    private void onRefreshData(){

        if(TextUtils.isEmpty(CacheConfig.getInst().getToken(this))){
            ToastUtil.showToast(this,"没有登录！");
            refresh.setRefreshing(false);
            return;
        }

        if(TextUtils.isEmpty(CacheConfig.getInst().getWaterToken(this))){
            ToastUtil.showToast(this,"请绑定工号！");
            refresh.setRefreshing(false);
            return;
        }


        Map<String,String> map = new HashMap<>();
        map.put(ConfigMethod.shop_token,CacheConfig.getInst().getToken(this));
        map.put(ConfigMethod.waiter_token,CacheConfig.getInst().getWaterToken(this));
        RetrofitFactory.getIntance().refreshData(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<LoginBean>() {
                    @Override
                    public void onCompleted() {
                        refresh.setRefreshing(false);
                    }

                    @Override
                    public void onError(Throwable e) {
                        refresh.setRefreshing(false);
                        emptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR,-1);
                    }

                    @Override
                    public void onNext(LoginBean loginBean) {
                        HttpUtils.check(loginBean, AllOrderActivity.this, loginBean1 -> {
                            oederList = OrderUtils.getOederList(null,0);

                            if(oederList.size() == 0){
                                emptyLayout.setVisibility(View.VISIBLE);
                                emptyLayout.setErrorType(EmptyLayout.NO_DATA,EmptyLayout.NOT_DATA);
                            }else{
                                emptyLayout.setVisibility(View.GONE);
                            }

                            tv_order_num.setText(oederList.size()+"");
                            double prce = 0;
                            for(int i = 0;i<oederList.size();i++){
                                OrderWapper orderWapper = oederList.get(i);
                                prce+=Double.parseDouble(orderWapper.getOrder_price());
                            }
                            tv_price.setText("¥"+prce);
                            adater.setNewData(oederList);
                        });
                    }
                });
    }
}
